home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3.iso / chapte10 / abantran.c next >
C/C++ Source or Header  |  1996-03-06  |  8KB  |  251 lines

  1.  
  2. #include <windows.h>  
  3. #include "abantran.h"  
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17.  
  18. LPCTSTR lpszAppName = "MyApp";
  19. LPCTSTR lpszTitle   = "DdeAbandanTransaction()"; 
  20.  
  21.  
  22. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  23.  
  24.  
  25. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  26.                       LPTSTR lpCmdLine, int nCmdShow)
  27. {
  28.    MSG      msg;
  29.    HWND     hWnd; 
  30.    WNDCLASS wc;
  31.  
  32.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  33.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  34.    wc.cbClsExtra    = 0;                      
  35.    wc.cbWndExtra    = 0;                      
  36.    wc.hInstance     = hInstance;              
  37.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  38.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  39.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  40.    wc.lpszMenuName  = lpszAppName;              
  41.    wc.lpszClassName = lpszAppName;              
  42.  
  43.    if ( IS_WIN95 )
  44.    {
  45.       if ( !RegisterWin95( &wc ) )
  46.          return( FALSE );
  47.    }
  48.    else if ( !RegisterClass( &wc ) )
  49.       return( FALSE );
  50.  
  51.    hInst = hInstance; 
  52.  
  53.    hWnd = CreateWindow( lpszAppName, 
  54.                         lpszTitle,    
  55.                         WS_OVERLAPPEDWINDOW, 
  56.                         CW_USEDEFAULT, 0, 
  57.                         CW_USEDEFAULT, 0,  
  58.                         NULL,              
  59.                         NULL,              
  60.                         hInstance,         
  61.                         NULL               
  62.                       );
  63.  
  64.    if ( !hWnd ) 
  65.       return( FALSE );
  66.  
  67.    ShowWindow( hWnd, nCmdShow ); 
  68.    UpdateWindow( hWnd );         
  69.  
  70.    while( GetMessage( &msg, NULL, 0, 0) )   
  71.    {
  72.       TranslateMessage( &msg ); 
  73.       DispatchMessage( &msg );  
  74.    }
  75.  
  76.    return( msg.wParam ); 
  77. }
  78.  
  79.  
  80. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  81. {
  82.    WNDCLASSEX wcex;
  83.  
  84.    wcex.style         = lpwc->style;
  85.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  86.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  87.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  88.    wcex.hInstance     = lpwc->hInstance;
  89.    wcex.hIcon         = lpwc->hIcon;
  90.    wcex.hCursor       = lpwc->hCursor;
  91.    wcex.hbrBackground = lpwc->hbrBackground;
  92.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  93.    wcex.lpszClassName = lpwc->lpszClassName;
  94.  
  95.    // Added elements for Windows 95.
  96.    //...............................
  97.    wcex.cbSize = sizeof(WNDCLASSEX);
  98.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  99.                             IMAGE_ICON, 16, 16,
  100.                             LR_DEFAULTCOLOR );
  101.             
  102.    return RegisterClassEx( &wcex );
  103. }
  104.  
  105. HDDEDATA CALLBACK DdemlCallback( UINT     uType,
  106.                                  UINT     uFmt,    
  107.                                  HCONV    hconv,    
  108.                                  HSZ      hsz1,    
  109.                                  HSZ      hsz2,    
  110.                                  HDDEDATA hdata,    
  111.                                  DWORD    dwData1,
  112.                                  DWORD    dwData2 );
  113.  
  114.  
  115. DWORD ddeInst    = 0;
  116. HSZ   hszTopic   = NULL;
  117. HSZ   hszService = NULL;
  118. HCONV hConv      = NULL;
  119. DWORD dwTranID;
  120.  
  121. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  122. {
  123.    switch( uMsg )
  124.    {
  125.       case WM_CREATE :
  126.               // Initialize DDEML.
  127.               //..................
  128.               if ( DdeInitialize( &ddeInst, DdemlCallback, 
  129.                    APPCMD_CLIENTONLY, 0 ) != DMLERR_NO_ERROR )
  130.               {
  131.                  MessageBox( hWnd, "Could not initialize DDEML.", 
  132.                              NULL, MB_OK | MB_ICONSTOP );
  133.                  return( -1 );
  134.               }
  135.  
  136.               // Allocate strings.
  137.               //..................
  138.               hszService = DdeCreateStringHandle( ddeInst,"Server",CP_WINANSI );
  139.               hszTopic   = DdeCreateStringHandle( ddeInst,"Msg",CP_WINANSI );
  140.               break;
  141.  
  142.       case WM_COMMAND :
  143.               switch( LOWORD( wParam ) )
  144.               {
  145.                  case IDM_TEST :
  146.                         // Connect to server.
  147.                         //...................
  148.                         hConv = DdeConnect( ddeInst, hszService, 
  149.                                             hszTopic, NULL );
  150.                         if ( hConv )
  151.                         {
  152.                            // Execute a DDE transaction on the 
  153.                            // DDEML server in asynchronous mode.
  154.                            //...................................
  155.                            HDDEDATA hRet = DdeClientTransaction( 
  156.                                                  "This is a sample message",
  157.                                                  37, hConv, 0L, 0, XTYP_EXECUTE,
  158.                                                  TIMEOUT_ASYNC,
  159.                                                  &dwTranID ); 
  160.  
  161.                            if ( !hRet )
  162.                            {
  163.                               DdeDisconnect( hConv );
  164.                               hConv = NULL;
  165.                            }
  166.                         }
  167.                         break;
  168.  
  169.                  case IDM_ABORT : 
  170.                         // If the conversation is active, abort the transaction.
  171.                         //......................................................
  172.                         if ( hConv )
  173.                         {
  174.                            DdeAbandonTransaction( ddeInst, hConv, dwTranID );
  175.                            DdeDisconnect( hConv );
  176.                            hConv = NULL;
  177.                         }
  178.                         break;
  179.  
  180.                  case IDM_ABOUT :
  181.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  182.                         break;
  183.  
  184.                  case IDM_EXIT :
  185.                         DestroyWindow( hWnd );
  186.                         break;
  187.               }
  188.               break;
  189.       
  190.       case WM_DESTROY :
  191.               if ( hConv )
  192.                  DdeDisconnect( hConv );
  193.                   
  194.               DdeFreeStringHandle( ddeInst, hszService );
  195.               DdeFreeStringHandle( ddeInst, hszTopic );
  196.               DdeUninitialize( ddeInst );
  197.               PostQuitMessage(0);
  198.               break;
  199.  
  200.       default :
  201.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  202.    }
  203.  
  204.    return( 0L );
  205. }
  206.  
  207.  
  208. HDDEDATA CALLBACK DdemlCallback( UINT     uType,
  209.                                  UINT     uFmt,    
  210.                                  HCONV    hconv,    
  211.                                  HSZ      hsz1,    
  212.                                  HSZ      hsz2,    
  213.                                  HDDEDATA hdata,    
  214.                                  DWORD    dwData1,
  215.                                  DWORD    dwData2 )
  216. {
  217.    switch( uType )
  218.    {
  219.       case XTYP_XACT_COMPLETE :
  220.             DdeDisconnect( hConv );
  221.             hConv = NULL;
  222.             break;
  223.    }
  224.  
  225.    return( NULL );
  226. }
  227.  
  228.  
  229. LRESULT CALLBACK About( HWND hDlg,           
  230.                         UINT message,        
  231.                         WPARAM wParam,       
  232.                         LPARAM lParam)
  233. {
  234.    switch (message) 
  235.    {
  236.        case WM_INITDIALOG: 
  237.                return (TRUE);
  238.  
  239.        case WM_COMMAND:                              
  240.                if (   LOWORD(wParam) == IDOK         
  241.                    || LOWORD(wParam) == IDCANCEL)    
  242.                {
  243.                        EndDialog(hDlg, TRUE);        
  244.                        return (TRUE);
  245.                }
  246.                break;
  247.    }
  248.  
  249.    return (FALSE); 
  250. }
  251.